package com.bjl.service.imp;

import com.alibaba.fastjson.JSONObject;
import com.bjl.dao.RoomDao;
import com.bjl.dao.UserDao;
import com.bjl.entity.Room;
import com.bjl.entity.User;
import com.bjl.service.RoomService;
import com.bjl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import orm.base.Ex.BaseServiceEx;
import orm.dictionary.Role;
import orm.dictionary.RoomType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 用户service
 * Created by ashin on 2017/11/9.
 */

@Service
@Scope("prototype")
public class RoomServiceImp extends BaseServiceEx implements RoomService {

    @Autowired
    RoomDao roomDao;

    @Autowired
    UserService userService;

    @Override
    public void init() {
        super.init(roomDao);
    }

    @Override
    public JSONObject queryNormalChartRooms(Map map,HttpSession session) throws Exception {
        //获得登录用户的权限
//        User user = (User)session.getAttribute("user");
//        User upperUser = userService.findTopReferrerId(user);
//        map.put("owner",upperUser.getId());
        map.put("type", String.valueOf(RoomType.normal));
        return queryChartRoomsByType(map);
    }

    @Override
    public JSONObject queryRecreationChartRooms(Map map,HttpSession session) throws Exception{
        //获得登录用户的权限
        User user = (User)session.getAttribute("user");
        String role = user.getRole();
        //如果是普通用户
        if(String.valueOf(Role.user).equals(role))
            return this.codeMess(eCode,"抱歉，您的权限不足");
        //如果是房管
        if(String.valueOf(Role.ow).equals(role)){
            map.put("owner",user.getId());
        }
        //如果是vip，需要向上查找对应的房管ID
        if(Role.withoutLeader(role)){
            User upperUser = userService.findTopReferrerId(user);
            map.put("owner",upperUser.getId());
        }
        map.put("type", String.valueOf(RoomType.recreation));
        return queryChartRoomsByType(map);
    }

    public JSONObject query(Map map,HttpSession session) throws Exception {
        User user = (User)session.getAttribute("user");
        String role = user.getRole();
        if(!Role.manageGroup(role))
            return this.codeMess(eCode,"抱歉，您的权限不足");
        if(!Role.roomManageGroup(role)){
            map.put("owner",user.getLeader());
        }else if(String.valueOf(Role.ow).equals(role)){
            map.put("owner",user.getId());
        }
        return super.query(map);
    }

    @Override
    public JSONObject pagequery(Map map,HttpSession session) throws Exception {
        User user = (User)session.getAttribute("user");
        String role = user.getRole();
        if(!Role.manageGroup(role))
            return this.codeMess(eCode,"抱歉，您的权限不足");
        if(!Role.roomManageGroup(role)){
            map.put("owner",user.getLeader());
        }else if(String.valueOf(Role.ow).equals(role))
            map.put("owner",user.getId());
        return super.pagequery(map);
    }

    @Override
    public JSONObject addUserRoom(Map map, HttpSession session) throws Exception {
        User user = (User)session.getAttribute("user");
        map.put("uId",user.getId());
        int ret = roomDao.addUserRoom(map);
        if(ret == -1){
            return this.codeMess(eCode,"新增失败");
        }
        return this.codeMess(sCode,"新增成功");
    }

    @Override
    public JSONObject removeUserRoom(Map map, HttpSession session) throws Exception {
        User user = (User)session.getAttribute("user");
        map.put("uId",user.getId());
        int ret = roomDao.removeUserRoom(map);
        if(ret == -1){
            return this.codeMess(eCode,"删除失败");
        }
        return this.codeMess(sCode,"删除成功");
    }

    private JSONObject queryChartRoomsByType(Map map) throws Exception{
        return this.codeData(roomDao.queryChartRoomsByType(map));
    }

}
